package com.pb.camera.jph.activity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import com.pb.camera.R;
import com.pb.camera.selectroom.spinner.SpinerPopWindow;
import com.pb.camera.selectroom.spinner.AbstractSpinerAdapter.IOnItemSelectListener;
import com.pb.camera.utils.Logger;
import com.pb.camera.utils.TaskHelper;
import com.pb.camera.work.ControlTask;
import com.pb.camera.work.IOTCInit;
import com.pb.camera.work.ReceiveTask;
import com.tutk.IOTC.AVIOCTRLDEFs;
import com.tutk.IOTC.CommApis;

import android.app.Activity;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class SettingCameraWifiActivity extends Activity implements OnClickListener, IOnItemSelectListener, Observer, TextWatcher {
    private static final String TAG = "SettingCameraWifiActivity";
    private ImageButton ib_back_add;
    private EditText et_wifi_name, et_wifi_password;
    private TextView tv_switch_wifi;
    private Button mBtnNext;
    private Checkable cb_save_password;

    private SpinerPopWindow mSpinerPopWindow;
    private WifiAdmin mWifiAdmin;
    private List<String> mWifiNamesList;
    private List<ScanResult> mListScanResult;
    private ScanResult mScanResult;
    private String mInputPassword;
    private String uuid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_camera_wifi);
        initView();
        initData();
        initLintener();
    }

    private void initView() {
        ib_back_add = (ImageButton) findViewById(R.id.ib_back_add);
        et_wifi_name = (EditText) findViewById(R.id.et_wifi_name);
        et_wifi_password = (EditText) findViewById(R.id.et_wifi_password);
        tv_switch_wifi = (TextView) findViewById(R.id.tv_switch_wifi);
        cb_save_password = (Checkable) findViewById(R.id.cb_save_password);
        mBtnNext = (Button) findViewById(R.id.btn_next);
        mSpinerPopWindow = new SpinerPopWindow(getApplicationContext());
        mSpinerPopWindow.setItemListener(this);
    }

    private void initData() {
        mWifiNamesList = new ArrayList<String>();
        mWifiAdmin = new WifiAdmin(this);
        TaskHelper.exec(new TaskHelper.Task() {
            @Override
            public void execute() throws Exception {
                //不知为何取不到返回的wifi数据
                mWifiAdmin.startScan();
                mWifiNamesList = mWifiAdmin.getWifiNames();
                mListScanResult = mWifiAdmin.getWifiList();
            }

            @Override
            public void callback(Exception e) {
                mSpinerPopWindow.refreshData(mWifiNamesList, 0);
            }
        });
    }

    private void initLintener() {
        ib_back_add.setOnClickListener(this);
        et_wifi_name.setOnClickListener(this);
        et_wifi_password.setOnClickListener(this);
        et_wifi_password.addTextChangedListener(this);
        mBtnNext.setOnClickListener(this);
        tv_switch_wifi.setOnClickListener(this);
        TaskHelper.exec(new Runnable() {
            @Override
            public void run() {
                uuid = getIntent().getStringExtra("UUID");
                if (uuid != null) {
                    IOTCInit iotcInit = new IOTCInit(uuid);
                    iotcInit.runInit();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_back_add:
                finish();
                break;
            case R.id.tv_switch_wifi:
                showSpinWindow();
                break;
            case R.id.btn_next:
                Intent intent = new Intent(this, ConnectCameraNetActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("wifiConfig", mScanResult);
                bundle.putString("wifiPassword", mInputPassword);
                bundle.putString("UUID",uuid);
                intent.putExtra("wifiInfo", bundle);
                this.startActivity(intent);
                break;
        }
    }

    private void showSpinWindow() {
        Log.e("", "showSpinWindow");
        mSpinerPopWindow.setWidth(et_wifi_name.getWidth());
        if (mWifiNamesList.size() > 10) {
            mSpinerPopWindow.setHeight(et_wifi_name.getWidth() * 2);
        }
        mSpinerPopWindow.showAsDropDown(et_wifi_name);
    }

    @Override
    public void onItemClick(int pos) {
        if (pos >= 0 && pos <= mWifiNamesList.size()) {
            String selectWifiName = mWifiNamesList.get(pos);
            et_wifi_name.setText(selectWifiName);
            mScanResult = mListScanResult.get(pos);
        }
    }

    @Override
    public void update(Observable observable, Object data) {
     /*   if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlListWifiApResp) {
            AVIOCTRLDEFs.SMsgAVIoctrlListWifiApResp wifiApResp = (AVIOCTRLDEFs.SMsgAVIoctrlListWifiApResp) data;
            //mWifiAps = wifiApResp.stWifiAps;
            for (int count = 0; count < wifiApResp.number; count++) {
                mWifiNamesList.add(CommApis.ByteToString(wifiApResp.stWifiAps[count].ssid));
                Logger.d(TAG, wifiApResp.stWifiAps[count].toString());
            }
        }
        if (mSpinerPopWindow != null)
            mSpinerPopWindow.refreshData(mWifiNamesList, 0);*/
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        mInputPassword = s.toString();
        mBtnNext.setEnabled(s.length() > 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
